This code defines a utility function called streamToOutput
that downloads a file from a URL or processes a stream and saves it to a local file. It handles directory creation and utilizes imported functions for file fetching and stream processing.
npm run import -- "test stream to output"
var fs = require('fs');
var path = require('path');
var importer = require('../Core');
var mkdirpSync = importer.import("mkdirp");
var fetchOrStream = importer.import("fetch file or stream");
// locally based utility for editing styles
function streamToOutput(fileUrl, bucketName, stream) {
var outputPath = path.join(path.resolve(process.env.PROJECT_OUTPUT),
fileUrl.replace(/\?.*/ig, ''));
mkdirpSync(path.dirname(outputPath));
var writeStream = fs.createWriteStream(outputPath);
return fetchOrStream(stream || fileUrl, writeStream)
.then(() => outputPath)
}
module.exports = streamToOutput;
// Import required modules
const fs = require('fs');
const path = require('path');
const { importCoreFunctions } = require('../Core');
// Define utility function to stream a file or URL to output
/**
* Streams a file or URL to the specified output path.
*
* @param {string} fileUrl The URL or path to the file to be streamed.
* @param {string} bucketName The name of the bucket (currently unused).
* @param {ReadableStream} stream The ReadableStream containing the file data (or null for file URL).
* @returns {Promise} The path to the streamed file.
*/
function streamToOutput(fileUrl, bucketName, stream = null) {
// Construct the output path
const outputPath = path.join(
path.resolve(process.env.PROJECT_OUTPUT),
fileUrl.replace(/\?.*/ig, '')
);
// Create the output directory if it doesn't exist
importCoreFunctions.mkdirpSync(path.dirname(outputPath));
// Create a write stream for the output file
const writeStream = fs.createWriteStream(outputPath);
// Stream the file or URL to the output file
return importCoreFunctions.fetchOrStream(stream, writeStream)
.then((outputPath) => outputPath);
}
// Export the utility function
module.exports = streamToOutput;
This code defines a utility function called streamToOutput
that takes a file URL, a bucket name, and an optional stream as input.
Here's a breakdown:
Dependencies:
fs
: Node.js built-in module for file system operations.path
: Node.js built-in module for working with file and directory paths.importer
: A custom module (likely located in ../Core
) used to import other functions.Imported Functions:
mkdirpSync
: Imported from importer
, likely a function to create directories recursively.fetchOrStream
: Imported from importer
, likely a function to fetch a file from a URL or process a stream.streamToOutput
Function:
fileUrl
: The URL of the file to download.bucketName
: The name of the bucket (likely used for context or potential future integration).stream
: An optional stream object containing the file data.fileUrl
and the PROJECT_OUTPUT
environment variable.mkdirpSync
to create any necessary parent directories for the output file.fs.createWriteStream
to write the downloaded data to the output file.fetchOrStream
to either fetch the file from the URL or process the provided stream, writing the data to the write stream.Export:
module.exports = streamToOutput;
: Makes the streamToOutput
function available for use in other modules.In essence, this code provides a reusable function for downloading files from URLs or processing streams and saving them to local files.